<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Propertytagtype extends Model_Base {

    protected $_name = 'property_tag_type';
    protected $_primaryKey = 'prp_tgtype_id';

    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('prtyp' => 'property_tag_type'))
                ->where('prtyp.prp_tgtype_publish != 2')
                ->where('prp_tgtype_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
     public function fetchEntryByREFId($id) {
        $select = $this->select()
                ->from(array('prtyp' => 'property_tag_type'))
                ->where('prtyp.prp_tgtype_publish != 2')
                ->where('prp_tgtype_ref_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchPropertytagtype($where = "", $limit = "", $offset = "0", $order = 'prp_tgtype_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('prtyp' => 'property_tag_type'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $finalArray = array();
            foreach($data->toArray() as $_property_type) {
                $finalArray[$_property_type['prp_tgtype_id']] = $_property_type['prp_tgtype_title'];
            }
            return $finalArray;
        } else {
            return array();
        }
    }
    
     public function fetchalltag($where = "", $limit = "", $offset = "0", $order = 'prp_tgtype_id', $sort = 'ASC', $group = 'prp_tgtype_ref_id') {
       $select = $this->select()
                ->from(array('prtyp' => 'property_tag_type'),array('*','lang_names' => new Zend_Db_Expr('GROUP_CONCAT(ln.lang_name)')))
                ->setIntegrityCheck(false)
                ->join(array('category' => 'property_tag_type_name'), "category.prp_typename_id = prtyp.prp_tgtype_type")
                ->join(array('subcategory' => 'prp_sub_tag'), "subcategory.prp_sub_tag_id = prtyp.prp_tgtype_sub_tag")
                ->join(array('ln' => 'languages'), "prtyp.prp_tgtype_lang_id = ln.lang_id")
                ->where('prtyp.prp_tgtype_publish != 2')
                ->where('prp_tgtype_title != ""' );
         if ($where != '') {
            $select->where($where);
        }
        if ($group != "") {
            $select->group($group);
        }
        $select->order($order . " " . $sort);
        $result = $this->fetchAll($select);
        #echo $select;exit;
        if (!empty($result)) {
            return $result->toArray();
        } else {
            return array();
        }
    }
    
    public function fetchcountalltag($where ) {
       $select = $this->select()
                ->from(array('prtyp' => 'property_tag_type'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('category' => 'property_tag_type_name'), "category.prp_typename_id = prtyp.prp_tgtype_type")
                ->join(array('subcategory' => 'prp_sub_tag'), "subcategory.prp_sub_tag_id = prtyp.prp_tgtype_sub_tag")
                ->join(array('ln' => 'languages'), "prtyp.prp_tgtype_lang_id = ln.lang_id")
                ->where('prtyp.prp_tgtype_publish != 2')
                ->where('prp_tgtype_title != ""' )
                ->group('prp_tgtype_ref_id');
         
       if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        
        return($rows->cnt);
    }
    
    public function fetchtagwithsubcategory($where){
          $mainTagType = new Model_Propertymaintagtype();
          $subTagType = new Model_Propertysubtagtype();
          
          $MainCategory = $mainTagType->fetchallmaintag();
         
          $finalArray = array();
          foreach ($MainCategory as $key =>$_property_type){ 
            //$finalArray[$_property_type['prp_typename_name']] = $_property_type['prp_typename_name'];
            //$SubCategory = $this->fetchalltag("prp_typename_name = '".$_property_type['prp_typename_name']."'","","",'','');
            $SubCategory = $subTagType -> fetchallsubtag (' prp_main_tag_id = '.$_property_type['prp_typename_id']);
            foreach ($SubCategory as $sub_key =>$_subproperty_type){ 
                $LabelTag = $this->fetchalltag($where.' and prp_tgtype_sub_tag = '.$_subproperty_type['prp_sub_tag_id'].' and prp_tgtype_type = '.$_property_type['prp_typename_id'].'');
                foreach ($LabelTag as $label_key =>$_label_type){ 
                    $finalArray[$_property_type['prp_typename_name']]['id'] = $_property_type['prp_typename_id'];
                    //$finalArray[$_property_type['prp_typename_name']]['subcategory'][$_subproperty_type['prp_sub_tag_id']] = $_subproperty_type['prp_sub_tag_name'];
            
                    $finalArray[$_property_type['prp_typename_name']]['subcategory'][$_subproperty_type['prp_sub_tag_name']]['sub_id'] = $_subproperty_type['prp_sub_tag_id'];
                    $finalArray[$_property_type['prp_typename_name']]['subcategory'][$_subproperty_type['prp_sub_tag_name']]['label_name'][$_label_type['prp_tgtype_id']] = $_label_type['prp_tgtype_title'];
                }
            }
          }
         
         return $finalArray;
     
    }
  
    
    
    
   
    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getSubcategoryCount($where) {
        
        $select = $this->select()
                ->from(array('prtyp' => 'property_tag_type'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('category' => 'property_tag_type_name'), "category.prp_typename_id = prtyp.prp_tgtype_type")
                ->join(array('subcategory' => 'prp_sub_tag'), "subcategory.prp_sub_tag_id = prtyp.prp_tgtype_sub_tag")
                ->join(array('ln' => 'languages'), "prtyp.prp_tgtype_lang_id = ln.lang_id")
                ->where('prtyp.prp_tgtype_publish != 2')
                ->where('prp_tgtype_title != ""' );
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    
    
    
    

}
